package LeetCode.exercise;

import java.util.Stack;

public class LC227 {
    public static void main(String[] args) {
        String s = "adsda";
        String t = s.substring(0, 2);
        System.out.println(s+ " " + t);
        LC227 test = new LC227();
        System.out.println(test.calculate("4-3+6*4/2-123+34"));
    }
    public int calculate(String s) {
        Stack<Integer> num = new Stack<>();
        Stack<Character> op = new Stack<>();
        op.push('+');
        for(int i = 0; i < s.length();){
            int j = i;
            while(j < s.length() && (s.charAt(j) >= '0' && s.charAt(j) <= '9')){
                j++;
            }
            int tmp = Integer.parseInt(s.substring(i, j));
            switch(op.peek()){
                case '+': {
                    num.push(tmp);
                    break;
                }
                case '-': {
                    num.push(-tmp);
                    break;
                }
                case '*': {
                    int res = num.pop() * tmp;
                    num.push(res);
                    break;
                }
                case '/': {
                    int res = num.pop() / tmp;
                    num.push(res);
                    break;
                }
            }
            op.pop();
            if(j < s.length()) {
                op.push(s.charAt(j));
            }
            i = j+1;
        }
        int ret = 0;
        while (!num.isEmpty()) {
            ret += num.pop();
        }
        return ret;
    }
}
